<template>
  <div class="app-container">
    <!-- 搜索筛选操作栏 -->
    <div class="filter-container clearfix">
      <el-date-picker
        v-model="dateInput"
        class="filter-item"
        type="datetimerange"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        :default-time="['00:00:00', '23:59:59']"
        style="max-width: 100%"
        @change="dateInputHandle"
      />
      <el-select
        v-model="type"
        placeholder="请选择"
        class="filter-item"
        style="margin-left: 10px; width: 100px"
        @change="selectHandle"
      >
        <el-option label="账号" :value="0" />
        <el-option label="姓名" :value="1" />
      </el-select>
      <el-input
        v-if="type == 0"
        v-model="listQuery.user"
        placeholder="请输入账号"
        style="width: 200px; margin-left: 10px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-if="type == 1"
        v-model="listQuery.name"
        placeholder="请输入姓名"
        style="width: 200px; margin-left: 10px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select
        v-model="listQuery.status"
        placeholder="请选择状态"
        class="filter-item"
        style="margin-left: 10px; width: 150px"
        @change="selectHandle"
      >
        <el-option label="全部状态" value="" />
        <el-option label="审核中" :value="0" />
        <el-option label="同意" :value="1" />
        <el-option label="拒绝" :value="2" />
        <el-option label="无效" :value="3" />
      </el-select>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        style="margin-left: 10px"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        v-waves
        class="filter-item"
        icon="iconfont qt-caozuo-xunhuan1"
        @click="clearFilter"
      >
        重置
      </el-button>
    </div>

    <!-- 表格 -->
    <el-table
      :key="tableKey"
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column label="账号" prop="user.user" align="center" />
      <el-table-column label="姓名" align="center" prop="user.name" />
      <el-table-column label="提现方式" header-align="center">
        <template slot-scope="{ row }">
          <div>
            {{
              row.type == 0
                ? "支付宝"
                : row.type == 1
                  ? "微信"
                  : row.type == 2
                    ? "银行卡"
                    : ""
            }}
          </div>
          <div v-if="row.user && row.type == 2">
            {{ row.user.bank_card_name }}({{ row.user.branch_name }})
          </div>
          <span v-if="row.user && row.type == 2">{{ row.user.bank_card }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提现金额" align="center" prop="money" />
      <el-table-column align="center" label="申请时间" prop="created_at" />
      <el-table-column label="提现状态" align="center">
        <template slot-scope="{ row }">
          <div>
            {{
              row.status == 0
                ? "审核中"
                : row.status == 1
                  ? "同意"
                  : row.status == 2
                    ? "拒绝"
                    : row.status == 3
                      ? "无效"
                      : ""
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="{ row }">
          <el-button
            v-if="row.status == 0"
            size="mini"
            plain
            type="danger"
            @click="handleDeal(row.id, { status: '1' })"
          >
            同意
          </el-button>
          <el-button
            v-if="row.status == 0"
            size="mini"
            plain
            type="warning"
            @click="handleDeal(row.id, { status: '2' })"
          >
            拒绝
          </el-button>
          <span v-if="row.status > 0">-</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div v-if="total > 0" style="text-align: right">
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.per_page"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { getWalletCashOut, updateWalletCashOut } from '@/api/common'
import { utcAdd } from '@/utils'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'FinanceWalletCashOut',
  components: {
    Pagination
  },
  directives: {
    waves
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      type: 0,
      dateInput: undefined,
      listQuery: {
        page: 1,
        per_page: 20,
        start_time: undefined,
        end_time: undefined,
        name: undefined,
        user: undefined,
        status: '',
        type: '2'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取时间
    dateInputHandle(e) {
      if (e && e.length > 0) {
        this.listQuery.start_time = utcAdd(e[0])
        this.listQuery.end_time = utcAdd(e[1])
      } else {
        this.listQuery.start_time = undefined
        this.listQuery.end_time = undefined
      }
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    clearFilter() {
      this.listQuery['start_time'] = undefined
      this.listQuery['end_time'] = undefined
      this.listQuery['name'] = undefined
      this.listQuery['user'] = undefined
      this.listQuery['status'] = ''
      this.dateInput = undefined
      this.handleFilter()
    },
    selectHandle(e) {
      if (e == 1) {
        this.listQuery['user'] = undefined
      } else {
        this.listQuery['name'] = undefined
      }
    },
    handleDeal(id, params) {
      const msg = params.status == '1' ? '同意' : '拒绝'
      this.$confirm(`确认${msg}提现?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          updateWalletCashOut(id, params).then(() => {
            this.$notify({
              title: '提示',
              message: '操作成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        })
        .catch(() => {})
    },
    getList() {
      this.listLoading = true
      getWalletCashOut(this.listQuery)
        .then((response) => {
          const { data, total } = response.data
          this.list = data
          this.total = total
        })
        .finally(() => {
          this.listLoading = false
        })
    }
  }
}
</script>
